package infovis.tasker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import org.mt4j.MTApplication;
import org.mt4j.components.MTCanvas;
import org.mt4j.components.MTComponent;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.font.IFont;
import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.MTTextArea.ExpandDirection;
import org.mt4j.components.visibleComponents.widgets.buttons.MTImageButton;
import org.mt4j.components.visibleComponents.widgets.buttons.MTSvgButton;
import org.mt4j.components.visibleComponents.widgets.keyboard.MTKeyboard;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;


public class MantemEstoria {
	
	public void adicionaEstoria(final MTApplication app, final IFont font2, MTImageButton estoriaButton,final String imagePath, final MTCanvas canvas,final List<MTEstoria> listaestorias,final int numero)
	{
		estoriaButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {
				switch (ae.getID()) {
				case TapEvent.BUTTON_CLICKED:
					//Flickr Keyboard
					String texto = "";
					final MTKeyboard keyb1 = new MTKeyboard(app);
					keyb1.setFillColor(new MTColor(30, 30, 30, 210));
					keyb1.setStrokeColor(new MTColor(0,0,0,255));
					keyb1.setSizeXYGlobal(300, 150);

					final MTTextArea t1 = new MTTextArea(app, FontManager.getInstance().createFont(app, "arial.ttf", 50, 
			        		new MTColor(0,0,0,255), //Fill color 
							new MTColor(0,0,0,255))); //Stroke color
			        t1.setExpandDirection(ExpandDirection.UP);
					
					t1.setStrokeColor(new MTColor(0,0 , 0, 255));
					t1.setFillColor(new MTColor(205,200,177, 255));
					t1.unregisterAllInputProcessors();
					t1.setEnableCaret(true);
					t1.snapToKeyboard(keyb1);
					for(MTEstoria estoria: listaestorias)
					{
						if(numero==estoria.getNumero())
						{
							texto = estoria.getTexto();
						}
					}
					t1.setText(texto);
					keyb1.addTextInputListener(t1);

					//Flickr Button for the keyboard
					MTSvgButton flickrButton1 = new MTSvgButton( imagePath + "Flickr_Logo.svg", app);
					flickrButton1.scale(0.4f, 0.4f, 1, new Vector3D(0,0,0), TransformSpace.LOCAL);
					flickrButton1.translate(new Vector3D(0, 15,0));
					flickrButton1.setBoundsPickingBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);

					//Add actionlistener to flickr button
					flickrButton1.addActionListener(new ActionListener() {

						public void actionPerformed(ActionEvent arg0) {

							if (arg0.getSource() instanceof MTComponent){
								//MTBaseComponent clickedComp = (MTBaseComponent)arg0.getSource();
								switch (arg0.getID()) {
								case TapEvent.BUTTON_CLICKED:

									System.out.println("Flickr search for: \"" + t1.getText() + "\"");
								
									MTEstoria estoria = new MTEstoria(t1.getText(), numero);
									
									listaestorias.add(estoria); 

									t1.clear();
									keyb1.destroy();
									break;
								default:
									break;
								}
							}
						}
					});
					keyb1.addChild(flickrButton1);

					canvas.addChild(keyb1);
					keyb1.setPositionGlobal(new Vector3D(app.width/2f, app.height/2f,0));

					break;

				default:
					break;
				}
			}
		});
	}

}
